package com.css.fxfzaqbz.modules.assess1.controller;

import com.css.fxfzaqbz.base.response.RestResponse;
import com.css.fxfzaqbz.modules.assess1.service.LittleRepotrService;
import com.css.fxfzaqbz.modules.assess1.entity.LittleReport;
import com.css.fxfzaqbz.modules.assess1.vo.*;
import com.css.fxfzaqbz.modules.data.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-12-13 10:03
 * 生成报告的接口
 */
@RestController
@Slf4j
@RequestMapping("fxfzaqbz/report")
public class ReportManagementController {



    @Resource
    private ReportService reportService;
    @Resource
    private LittleRepotrService littleRepotrService;
    /***
     * （重新）生成报告
     *
     * @return
     */
    @PostMapping("/createReport")
    public RestResponse createReport(@RequestBody ReportVO reportVO, HttpServletResponse response ) throws Exception {
        return reportService.createReport(reportVO,response);
    }

    //新增
    @PostMapping("/save")
    public RestResponse save(SaveReportVO saveReportVO) {
        RestResponse restResponse = null;
        try {
            littleRepotrService.save(saveReportVO);
            restResponse = RestResponse.succeed("上传成功");
        } catch (Exception e) {
            log.error("上传失败", e);
            restResponse = RestResponse.fail("上传失败!");
        }
        return restResponse;
    }

    //查询
    @GetMapping("/getAll")
    public RestResponse getAll(LittleReportVO littleReportVO) {
        RestResponse response = null;
        try {
            Map<String, Object> all = littleRepotrService.getAll(littleReportVO);
            response = RestResponse.succeed(all);
        } catch (Exception e) {
            log.error("查询报告失败!", e);
            response = RestResponse.fail("查询报告失败!");
        }
        return response;
    }
    //查询
    @GetMapping("/getById/{id}")
    public RestResponse getById(@PathVariable("id") String id) {
        RestResponse response = null;
        try {
            LittleReport  littleReport= littleRepotrService.getById(id);
            response = RestResponse.succeed(littleReport);
        } catch (Exception e) {
            log.error("查询失败!", e);
            response = RestResponse.fail("查询失败!");
        }
        return response;
    }



    //提交 小报告
    @PostMapping("/submit")
    public RestResponse submit(@RequestBody SubmitLittleRepoetVO submitLittleRepoetVO) {
        RestResponse response = null;
        try {
         littleRepotrService.submit(submitLittleRepoetVO);
            response = RestResponse.succeed();
        } catch (Exception e) {
            log.error("提交失败!", e);
            response = RestResponse.fail("提交失败!");
        }
        return response;
    }

    //退回 小报告
    @GetMapping("/back/{id}")
    public RestResponse back(@PathVariable("id") String id) {
        RestResponse response = null;
        try {
            littleRepotrService.back(id);
            response = RestResponse.succeed();
        } catch (Exception e) {
            log.error("退回失败!", e);
            response = RestResponse.fail("退回失败!");
        }
        return response;
    }



    //合并报告页面
    @GetMapping("/getLittleReport/{iaId}")
    public RestResponse getLittleReport(@PathVariable("iaId") String iaId) {
        RestResponse response = null;
        try {
            Map<String, Object> map= littleRepotrService.getLittleReport(iaId);
            response = RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("查询报告失败!", e);
            response = RestResponse.fail("查询报告失败!");
        }
        return response;
    }

    //合并报告
    @PostMapping("/mergeReport")
    public RestResponse mergeReport(@RequestBody MergeVO mergeVO,HttpServletResponse response) {
        return reportService.mergeReport(mergeVO.getList(),response);

    }



    //删除
    @GetMapping("/delete/{id}")
    public RestResponse delete(@PathVariable("id")String id) {
        RestResponse response = null;
        try {
            littleRepotrService.delete(id);
            response = RestResponse.succeed();
        } catch (Exception e) {
            log.error("删除失败!", e);
            response = RestResponse.fail("删除失败!");
        }
        return response;
    }

    //校验重复接口  true为没有重复  false为有重复
    @PostMapping("/check")
    public RestResponse check(@RequestBody ReportVO reportVO) {
        RestResponse response = null;
        try {
            String number = reportVO.getNumber();
            String iaId = reportVO.getIaId();
            boolean flag = littleRepotrService.check(iaId,number);
            response = RestResponse.succeed(flag);
        } catch (Exception e) {
            log.error("校验失败", e);
            response = RestResponse.fail("校验失败");
        }
        return response;
    }

    //PDF展示接口
    @GetMapping("/example/{sign}")
    public RestResponse example(HttpServletResponse res,@PathVariable String sign) {
        RestResponse response = null;
        try {
            littleRepotrService.pdf(res,sign);
            response=RestResponse.succeed();
            return response;
        } catch (Exception e) {
            log.error("校验失败", e);
            response = RestResponse.fail("校验失败");
        }
        return response;
    }

}
